﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Weixin.BLL.ChinaTax;
using Weixin.Entity.ChinaTax;
using Weixin.Utils;
using System.Text;

namespace Weixin.Web
{
    public partial class Article_txt : System.Web.UI.Page
    {
        int _Id;
        public int _TypeId;
        public CourseEntity _CourseEntity;
        public typeEntity _TypeEntity;

        #region jssdk
        protected string noncestr = string.Empty;
        protected string jsapi_ticket = string.Empty;
        protected string timestamp = string.Empty;
        protected string signature = string.Empty;

        protected string url = string.Empty;
        protected string title = string.Empty;
        protected string desc = string.Empty;
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            int.TryParse(Request.QueryString["Id"], out _Id);
            int.TryParse(Request.QueryString["TypeId"],out _TypeId);

            if (!string.IsNullOrEmpty(Request.QueryString["asyn"]) && Request.QueryString["asyn"] == "true")
            {
                AjaxReponse();
            }

            GetJssdk();
            GetType();
            GetData();
        }

        protected void AjaxReponse()
        {
            if (Request.QueryString["ope"] == "search")
            {
                string result = string.Empty;
                if (_Id == 156)
                {
                    YearMeetingBLL meetingBLL = YearMeetingBLL.GetInstance();
                    StringBuilder strBuilder = new StringBuilder();
                    List<YearMeetingEntity> allList = new List<YearMeetingEntity>();
                    List<YearMeetingEntity> list = meetingBLL.GetYearMeetingList() as List<YearMeetingEntity>;
                    foreach (YearMeetingEntity entity in list)
                    {
                        if (allList.FindAll(n => n.Name == entity.Name).Count == 0)
                            allList.Add(entity);
                    }
                    //将报名信息按照时间排序
                    allList = allList.OrderByDescending(n => n.CreateTime).ToList();
                    //将报名信息拼接成json供前端使用
                    foreach (YearMeetingEntity entity in allList)
                    {
                        if (strBuilder.ToString() != "")
                        {
                            strBuilder.Append(",");
                        }
                        strBuilder.Append("{\"name\":\"" + entity.Name + "\",\"time\":\"" + entity.CreateTime.ToString("MM-dd") + "\"}");
                    }

                    if (strBuilder.ToString() != "")
                    {
                        result = "[" + strBuilder.ToString() + "]";
                    }
                }
                else
                {
                    ApplicationsBLL appBLL = ApplicationsBLL.GetInstance();
                    StringBuilder strBuilder = new StringBuilder();
                    List<ApplicationsEntity> allList = new List<ApplicationsEntity>();
                    string ids = string.Empty;
                    if (_Id == 117 || _Id == 118 || _Id == 119)
                    {
                        ids = "117,118,119";
                    }
                    else if (_Id == 122 || _Id == 126)
                    {
                        ids = "122,126";
                    }
                    else if (_Id == 141 || _Id == 140 || _Id == 137 || _Id == 136 || _Id == 133 || _Id == 131 || _Id == 129 || _Id == 112)
                    {
                        ids = "141,140,137,136,133,131,129,112";
                    }
                    else
                    {
                        ids = _Id.ToString();
                    }
                    Logger.Log4Net.Info("获取信息：" + ids);
                    //根据课程Id字符串获取去重报名信息
                    List<ApplicationsEntity> list = appBLL.GetApplicationsList(ids) as List<ApplicationsEntity>;
                    list = list.FindAll(n => n.Status != 2);
                    foreach (ApplicationsEntity entity in list)
                    {
                        if (allList.FindAll(n => n.Name == entity.Name).Count == 0)
                            allList.Add(entity);
                    }
                    //将报名信息按照时间排序
                    allList = allList.OrderByDescending(n => n.CreateTime).ToList();
                    //将报名信息拼接成json供前端使用
                    foreach (ApplicationsEntity entity in allList)
                    {
                        if (strBuilder.ToString() != "")
                        {
                            strBuilder.Append(",");
                        }
                        strBuilder.Append("{\"name\":\"" + entity.Name + "\",\"time\":\"" + entity.CreateTime.ToString("MM-dd") + "\"}");
                    }

                    if (strBuilder.ToString() != "")
                    {
                        result = "[" + strBuilder.ToString() + "]";
                    }
                }
                Response.Write(result);
                Response.End();
            }

            if (Request.QueryString["ope"] == "view")
            {
                UserViewHistoryBLL userViewHistoryBLL = UserViewHistoryBLL.GetInstance();
                UserViewHistoryEntity userViewHistoryEntity = new UserViewHistoryEntity();
                userViewHistoryEntity.CreateTime = DateTime.Now;
                userViewHistoryEntity.IP = IPHelper.GetClientIP();
                userViewHistoryEntity.ObjId = _Id;
                userViewHistoryEntity.UserId = 0;
                userViewHistoryEntity.SourceId = 2;
                userViewHistoryEntity.OperateWay = 1;
                userViewHistoryEntity.ObjType = 3;
                userViewHistoryBLL.Insert(userViewHistoryEntity);
                Response.End();
            }

            if (Request.QueryString["ope"] == "share")
            {
                UserViewHistoryBLL userViewHistoryBLL = UserViewHistoryBLL.GetInstance();
                UserViewHistoryEntity userViewHistoryEntity = new UserViewHistoryEntity();
                userViewHistoryEntity.CreateTime = DateTime.Now;
                userViewHistoryEntity.IP = IPHelper.GetClientIP();
                userViewHistoryEntity.ObjId = _Id;
                userViewHistoryEntity.UserId = 0;
                userViewHistoryEntity.SourceId = 2;
                userViewHistoryEntity.OperateWay = 2;
                userViewHistoryEntity.ObjType = 3;
                userViewHistoryBLL.Insert(userViewHistoryEntity);
                Response.End();
            }
        }

        protected void GetJssdk()
        {
            WX wx = new WX();
            url = Request.Url.ToString().IndexOf("#") > -1 ? Request.Url.ToString().Substring(0, Request.Url.ToString().IndexOf("#")) : Request.Url.ToString();
            string[] arr = wx.GetJSSDK(url);
            noncestr = arr[0];
            jsapi_ticket = arr[1];
            timestamp = arr[2];
            signature = arr[3];
        }

        void GetType()
        {
            typeBLL bll = typeBLL.GetInstance();
            typeEntity entity = bll.GetAdminSingle(_TypeId);
            _TypeEntity = entity;
        }

        void GetData()
        {
            CourseBLL courseBLL = CourseBLL.GetInstance();
            _CourseEntity = courseBLL.GetAdminSingle(_Id);
            if (_CourseEntity == null)
            {
                _CourseEntity = new CourseEntity();
            }
            title = _CourseEntity.Title;
            desc = _CourseEntity.Title;
        }
    }
}